package net.taviscaron.drivenotes.android.activity;

import net.taviscaron.drivenotes.android.R;
import net.taviscaron.drivenotes.android.service.ChargingService;
import net.taviscaron.drivenotes.shared.model.entity.Car;
import net.taviscaron.drivenotes.shared.model.entity.Charging;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ChargingActivity extends ListActivity {
    private Car car;
    private ChargingListAdapter adapter;
    private ChargingService chargingService;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new ChargingListAdapter();
        chargingService = new ChargingService(this);
        car = (Car)getIntent().getSerializableExtra("car");
        setListAdapter(adapter);
    }
    
    @Override
    protected void onStart() {
	super.onStart();
	if(adapter.getCharging() == null) {
	    if(chargingService.isOnline()) {
		new ChargingLoader().execute();
	    } else {
		Toast.makeText(this, "You need an internet connection", Toast.LENGTH_SHORT).show();
	    }
	}
    }
    
    private class ChargingListAdapter extends BaseAdapter {
	private Charging[] charging;
	
	public void setCharging(Charging[] charging) {
	    this.charging = charging;
	    notifyDataSetChanged();
	}
	
	public Charging[] getCharging() {
	    return charging;
	}
	
	@Override
	public int getCount() {
	    return (charging != null) ? charging.length : 0;
	}

	@Override
	public Charging getItem(int position) {
	    return charging[position];
	}

	@Override
	public long getItemId(int position) {
	    return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup viewGroup) {
	    if(view == null) {
		LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = layoutInflater.inflate(R.layout.charging_item, null);
	    }
	    
	    Charging charging = getItem(position);
	    
	    ((TextView)view.findViewById(R.id.chargingVolume)).setText(charging.getVolume().toString());
	    ((TextView)view.findViewById(R.id.chargingPrice)).setText(charging.getPrice().toString());
	    ((TextView)view.findViewById(R.id.chargingDate)).setText(charging.getDate().toString());
	    ((TextView)view.findViewById(R.id.chargingFuelStation)).setText(charging.getFillingStation());

	    return view;
	}
    }
    
    private class ChargingLoader extends AsyncTask<Void, Void, Charging[]> {
	private ProgressDialog dialog;
	@Override
	protected Charging[] doInBackground(Void... args) {
	    return chargingService.loadCharging(car);
	}
	
	@Override
	protected void onPreExecute() {
	    dialog = ProgressDialog.show(ChargingActivity.this, "", "Loading...");
	}
	
	@Override
	protected void onPostExecute(Charging[] result) {
	    dialog.dismiss();
	    if(result == null || result.length == 0) {
		Toast.makeText(ChargingActivity.this, "There is no charging for this car", Toast.LENGTH_LONG).show();
	    } else {
		adapter.setCharging(result);
	    }
	}
    }
}
